﻿using System.Text;
using System.Web.UI;
using System.Xml.Linq;

namespace SPAdaptiveMenu.Controls
{
    /// <summary>
    /// Navigation menu item
    /// </summary>
    public class MenuLink : MenuItem
    {
        /// <summary>
        /// URL to navigate to on click
        /// </summary>
        public string Url { get; set;}

        public override MenuItemType ItemType
        {
            get { return MenuItemType.Option; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            var sb = new StringBuilder();
            sb.AppendFormat(@"<ie:menuitem id=""{0}"" type=""option""", ClientID);
            if (!string.IsNullOrEmpty(Url))
                sb.AppendFormat(@" onMenuClick=""javascript:GotoPage('{0}');""", Url);
            if (!string.IsNullOrEmpty(Title))
                sb.AppendFormat(@" title=""{0}""", Title);
            if (!string.IsNullOrEmpty(IconToolTip))
                sb.AppendFormat(@" iconAltText=""{0}""", IconToolTip);
            if (!string.IsNullOrEmpty(IconSrc))
                sb.AppendFormat(@" iconSrc=""{0}""", IconSrc);
            if (!string.IsNullOrEmpty(Description))
                sb.AppendFormat(@" description=""{0}""", Description);
            if (!string.IsNullOrEmpty(Title))
                sb.AppendFormat(@" text=""{0}""", Title);
            if (Disabled)
                sb.Append(@" enabled=""false""");
            if (Hidden)
                sb.Append(@" hidden=""true""");
            sb.Append("></ie:menuitem>");
            writer.Write(sb.ToString());
        }

        protected internal override XElement Serialized
        {
            get
            {
                var element = base.Serialized;
                element.Add(new XAttribute("Type", ItemType));
                element.Add(new XAttribute("Url", Url ?? string.Empty));
                return element;
            }
        }

        protected override object SaveControlState()
        {
            base.SaveControlState();
            var obj = base.SaveControlState();
            return obj != null
                ? (object)new Pair(obj, Serialized.ToString())
                : Serialized.ToString();
        }

        protected override void LoadControlState(object state)
        {
            if (state == null) return;
            var p = state as Pair;
            if (p != null)
            {
                base.LoadControlState(p.First);
                InitProperties(XElement.Parse((string)p.Second));
            }
            else
            {
                if (state is string)
                    InitProperties(XElement.Parse((string)state));
                else
                    base.LoadControlState(state);
            }
        }

        protected internal override void InitProperties(XElement element)
        {
            if (element == null) return;
            base.InitProperties(element);
            Url = element.Attribute("Url").Value;
        }

        protected internal override XElement HTML
        {
            get
            {
                var element = base.HTML;
                var onclick = string.Format("javascript:GotoPage('{0}');", Url);
                element.Add(new XAttribute("type", "option"));
                element.Add(new XAttribute("onMenuClick", onclick));
                return element;
            }
        }
    }
}
